// Tokenizing layer of parser

export class Tokenizer {

    // command source code
    private mSource: string;

    // output tokens
    private mTokens: Array<string>;

    // currently parsed token
    private mCurrentToken: string;

    // constructor
    public constructor() {
        this.mTokens = new Array<string>();
        this.mCurrentToken = "";
    }

    // property getter for token array
    public get Tokens(): Array<string> {
        return this.mTokens;
    }

    // the main parsing method
    public Parse(source: string): void {
        // set the source
        this.mSource = source;   
        this.mCurrentToken = "";

        // iterate all characters
        while (this.mSource.length !== 0) {
            // get and remove the first character
            var char = this.mSource[0];
            this.mSource = this.mSource.substr(1);

            // check whitespace
            if (this.IsWhitespace(char) == true) {
                // if yes and we have token, add it
                if (this.mCurrentToken.length > 0) {
                    this.AddCurrentToken();
                }
            }
            else {
                // else add to sign to the current token
                this.mCurrentToken += char;
            }
        }

        // check the last character
        if (this.mCurrentToken.length > 0) {
            this.AddCurrentToken();
        }
    }

    // check if whitespace
    private IsWhitespace(s: string): boolean {
        if (s.length !== 1)
            throw "Not a character";
        if (s == "\n" || s == "\r" || s == " " || s == "\t" || s == "\b")
            return true;
        else
            return false;
    }

    // adds the current token
    private AddCurrentToken() {
        if (this.mCurrentToken !== "") {
            this.mTokens.push(this.mCurrentToken);
            this.mCurrentToken = "";
        }
    }
}
